package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 套餐新增
     *
     * @param setmealDTO
     * @return
     */
    @Transactional
    @Override
    public Result setSetmeal(SetmealDTO setmealDTO) {
        //非空判断
        if (setmealDTO == null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //新增套餐
        setmealMapper.inseert(setmeal);


        return Result.success();
    }


    /**
     * 套餐分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result setmealPage(SetmealPageQueryDTO dto) {
        //非空判断
        if (dto.getPage() != null) {
            dto.setPage(1);
        }
        if (dto.getPageSize() != null) {
            dto.setPage(10);
        }

        //开启分页查询
        PageHelper.startPage(dto.getPage(), dto.getPageSize());

        //调用mapper层获取数据
        Page<SetmealVO> setmealVOS = setmealMapper.PageStart(dto);

        //封装数据
        PageResult pageResult = new PageResult();
        pageResult.setTotal(setmealVOS.getTotal());
        pageResult.setRecords(setmealVOS.getResult());

        return Result.success(pageResult);
    }
}
